local load = true

local CW = CreateFrame"Frame"

CW.name = "AddOn Chat-Message-Hider"
CW.funcs = {}
CW.funcs.FontString = function(Text, Size, AnchorTable)
	local String = CW:CreateFontString()
	--tinsert(CW, { Text = Text, Size = Size } )
	String:SetFont("Fonts\\FRIZQT__.TTF", Size)
	String:SetText(Text)
	if AnchorTable then
		if not AnchorTable.p then
			AnchorTable.p = "TOPLEFT"
		end
		if not AnchorTable.rP then
			AnchorTable.rP = "TOPLEFT"
		end
		String:SetPoint(AnchorTable.p, AnchorTable.rF, AnchorTable.rP, AnchorTable.x or 0, AnchorTable.y or 0)
	end
	return String
end
CW.funcs.Frame = function(frameType, frameName, parentFrame, inheritsFrame, AnchorTable)
	local Frame = CreateFrame(frameType, frameName, parentFrame, inheritsFrame)
	if AnchorTable then
		if not AnchorTable.p then
			AnchorTable.p = "TOPLEFT"
		end
		if not AnchorTable.rP then
			AnchorTable.rP = "TOPLEFT"
		end
		Frame:SetPoint(AnchorTable.p, AnchorTable.rF, AnchorTable.rP, AnchorTable.x or 0, AnchorTable.y or 0)
	end
	return Frame
end

CW.CheckButton5 = CW.funcs.Frame("CheckButton", nil, CW, "InterfaceOptionsCheckButtonTemplate", {rF = CW, x = 5, y = -30})
CW.CheckButton5.FontString = CW.funcs.FontString("Use Filter Method.", 12, {p = "LEFT", rF = CW.CheckButton5, rP = "RIGHT", x = 2})
CW.CheckButton5:SetScript("OnClick", function(self) if not self:GetChecked() and not CW.CheckButton6:GetChecked() then self:SetChecked(true) end end)
CW.CheckButton5:SetScript("OnEnter", function(self) GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT") GameTooltip:SetText"Will MAYBE not work while using other Chat-Filter-Addons." GameTooltip:Show() end)

CW.CheckButton6 = CW.funcs.Frame("CheckButton", nil, CW, "InterfaceOptionsCheckButtonTemplate", {rF = CW.CheckButton5, y = -20})
CW.CheckButton6.FontString = CW.funcs.FontString("Use Hook Method.", 12, {p = "LEFT", rF = CW.CheckButton6, rP = "RIGHT", x = 2})
CW.CheckButton6:SetScript("OnClick", function(self) if not self:GetChecked() and not CW.CheckButton5:GetChecked() then self:SetChecked(true) end end)
CW.CheckButton6:SetScript("OnEnter", function(self) GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT") GameTooltip:SetText"Will MAYBE not work while using a Chat-FrameAddon." GameTooltip:Show() end)

CW.CheckButton1 = CW.funcs.Frame("CheckButton", nil, CW, "InterfaceOptionsCheckButtonTemplate", {rF = CW.CheckButton6, y = -20})
CW.CheckButton1.FontString = CW.funcs.FontString("Hide every chat message from add ons.", 12, {p = "LEFT", rF = CW.CheckButton1, rP = "RIGHT", x = 2})

CW.CheckButton2 = CW.funcs.Frame("CheckButton", nil, CW, "InterfaceOptionsCheckButtonTemplate", {rF = CW.CheckButton1, y = -20})
CW.CheckButton2.FontString = CW.funcs.FontString("Hide chat messages from add ons while loading.", 12, {p = "LEFT", rF = CW.CheckButton2, rP = "RIGHT", x = 2})

CW.CheckButton3 = CW.funcs.Frame("CheckButton", nil, CW, "InterfaceOptionsCheckButtonTemplate", {rF = CW.CheckButton2, y = -20})
CW.CheckButton3.FontString = CW.funcs.FontString("Hide chat messages from add ons", 12, {p = "LEFT", rF = CW.CheckButton3, rP = "RIGHT", x = 2})
CW.CheckButton3.EditBox = CW.funcs.Frame("EditBox", nil, CW, nil, {p = "LEFT", rF = CW.CheckButton3.FontString, rP = "RIGHT", x = 1})
CW.CheckButton3.EditBox:SetBackdrop{ bgFile="Interface/ChatFrame/ChatFrameBackground" }
CW.CheckButton3.EditBox:SetBackdropColor(0.1,0.1,0.1,1)
CW.CheckButton3.EditBox:SetNumeric(true)
CW.CheckButton3.EditBox:SetJustifyH("CENTER")
CW.CheckButton3.EditBox:SetMaxLetters(2)
CW.CheckButton3.EditBox:SetWidth(25)
CW.CheckButton3.EditBox:SetHeight(15)
CW.CheckButton3.EditBox:SetFont("Fonts\\FRIZQT__.TTF", 11)
CW.CheckButton3.EditBox:SetAutoFocus(false)
CW.CheckButton3.EditBox:SetScript("OnEscapePressed", function(self) self:ClearFocus() end)
CW.CheckButton3.FontString2 = CW.funcs.FontString("seconds after loading.", 12, {p = "LEFT", rF = CW.CheckButton3.EditBox, rP = "RIGHT", x = 2})

CW.CheckButton4 = CW.funcs.Frame("CheckButton", nil, CW, "InterfaceOptionsCheckButtonTemplate", {rF = CW.CheckButton3, y = -20})
CW.CheckButton4.FontString = CW.funcs.FontString("Move hidden chat messages to ", 12, {p = "LEFT", rF = CW.CheckButton4, rP = "RIGHT", x = 2})
CW.CheckButton4.DropDown = CW.funcs.Frame("Frame", "ACMHChooseChatFrameDropDownMenu", CW, "UIDropDownMenuTemplate", {p = "LEFT", rF = CW.CheckButton4.FontString, rP = "RIGHT", x = -15, y = -2})
CW.CheckButton4.DropDown.Button = CW.CheckButton4.DropDown:GetChildren()
CW.CheckButton4.DropDown.Initfunc = function()
	if FCF_GetNumActiveChatFrames() > 2 then
		for i=3, FCF_GetNumActiveChatFrames() do
			local ChatWindowName = GetChatWindowInfo(i)
			UIDropDownMenu_AddButton{
				text = ChatWindowName, 
				value = ChatWindowName, 
				func = function(self) UIDropDownMenu_SetSelectedID(CW.CheckButton4.DropDown, self:GetID(), 1) CW.CheckButton4.DropDown.value = self:GetID() end
			}
		end
		CW.CheckButton4:Enable()
		CW.CheckButton4.FontString:SetTextColor(HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b)
		UIDropDownMenu_EnableDropDown(CW.CheckButton4.DropDown)
		UIDropDownMenu_SetSelectedID(CW.CheckButton4.DropDown, CW.CheckButton4.DropDown.value, 1)
	else
		UIDropDownMenu_DisableDropDown(CW.CheckButton4.DropDown)
		CW.CheckButton4:Disable()
		UIDropDownMenu_SetText(CW.CheckButton4.DropDown, "<No Chat Frames>")
		CW.CheckButton4.FontString:SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b)
	end
end
UIDropDownMenu_Initialize(CW.CheckButton4.DropDown, CW.CheckButton4.DropDown.Initfunc)

--Detect whenever a new chat frame is made
local oldFCF_OpenNewWindow = FCF_OpenNewWindow
function FCF_OpenNewWindow(...)
	local returnvalue = oldFCF_OpenNewWindow(...)
	UIDropDownMenu_Initialize(CW.CheckButton4.DropDown, CW.CheckButton4.DropDown.Initfunc)
	return returnvalue
end

--  [[ Bad Words ]]
CW.BadWords = {}

local function RefreshBadFontStrings()
	for index, value in pairs(ACMHOptions.badwords) do
		if CW.BadWords.Words[index] then
			CW.BadWords.Words[index]:Show()
			CW.BadWords.Words[index]:SetText(value)
		else
			CW.BadWords.Words[index] = CW.funcs.Frame("Button", nil, CW, "OptionsButtonTemplate")
			CW.BadWords.Words[index]:SetText(value)
			CW.BadWords.Words[index]:SetPoint("TOPLEFT", CW.BadWords.Words[index-1] or CW.BadWords.Words.Title, "BOTTOMLEFT", 0, -2)
			CW.BadWords.Words[index]:SetScript("OnClick", function() tremove(ACMHOptions.badwords, index) RefreshBadFontStrings() end)
		end
	end
	for i = (#ACMHOptions.badwords + 1), (#CW.BadWords.Words) do
		CW.BadWords.Words[i]:Hide()
	end
end

CW.BadWords.Title = CW.funcs.FontString("Bad Words", 20, {rF = CW.CheckButton4, rP = "BOTTOMLEFT", y = -10})
CW.BadWords.Subtitle = CW.funcs.FontString("Messages from Add Ons which include a bad word won't be shown.", 7, {rF = CW.BadWords.Title, rP = "BOTTOMLEFT", y = -2})
CW.BadWords.Subtitle:SetJustifyH"LEFT"
CW.BadWords.Subtitle:SetWidth(200)
CW.BadWords.EditBox = CW.funcs.Frame("EditBox", nil, CW, nil, {rF = CW.BadWords.Subtitle, rP = "BOTTOMLEFT", y = -12})
CW.BadWords.EditBox:SetWidth(100)
CW.BadWords.EditBox:SetHeight(15)
CW.BadWords.EditBox:SetBackdrop{ bgFile="Interface/ChatFrame/ChatFrameBackground" }
CW.BadWords.EditBox:SetBackdropColor(0.1,0.1,0.1,1)
CW.BadWords.EditBox:SetFont("Fonts\\FRIZQT__.TTF", 11)
CW.BadWords.EditBox:SetAutoFocus(false)
CW.BadWords.EditBox:SetScript("OnEscapePressed", function(self) self:ClearFocus() end)
CW.BadWords.EditBox.Label = CW.funcs.FontString("Add a word", 9, { p = "BOTTOMLEFT", rF = CW.BadWords.EditBox, y = 2})
CW.BadWords.EditBox.Button = CW.funcs.Frame("Button", nil, CW, "OptionsButtonTemplate", {p = "LEFT", rF = CW.BadWords.EditBox, rP = "RIGHT", x = 1})
CW.BadWords.EditBox.Button:SetWidth(30)
CW.BadWords.EditBox.Button:SetText("Add")
CW.BadWords.EditBox.Button:Hide()
CW.BadWords.EditBox.Button:SetScript("OnClick", function(self) self:Hide() tinsert(ACMHOptions.badwords, CW.BadWords.EditBox:GetText()) CW.BadWords.EditBox:SetText"" CW.BadWords.EditBox:ClearFocus() RefreshBadFontStrings() end)
CW.BadWords.EditBox:SetScript("OnTextChanged", function(self) if self:GetText() == "" then CW.BadWords.EditBox.Button:Hide() else CW.BadWords.EditBox.Button:Show() end end)

CW.BadWords.Words = {}
CW.BadWords.Words.Title = CW.funcs.FontString("Click on a word to delete it.", 7, {rF = CW.BadWords.EditBox, rP = "BOTTOMLEFT", y = -5})
CW.BadWords.Words.frames = {}
--//Bad Words


-- [[ Good Words ]]
CW.GoodWords = {}

local function RefreshGoodFontStrings()
	for index, value in pairs(ACMHOptions.goodwords) do
		if not CW.GoodWords.Words[index] then
			CW.GoodWords.Words[index] = CW.funcs.Frame("Button", nil, CW, "OptionsButtonTemplate")
			CW.GoodWords.Words[index]:SetPoint("TOPLEFT", CW.GoodWords.Words[index-1] or CW.GoodWords.Words.Title, "BOTTOMLEFT", 0, -2)
			CW.GoodWords.Words[index]:SetScript("OnClick", function() tremove(ACMHOptions.goodwords, index) RefreshGoodFontStrings() end)
		end
		CW.GoodWords.Words[index]:Show()
		CW.GoodWords.Words[index]:SetText(value)
	end
	for i = #ACMHOptions.goodwords + 1, #CW.GoodWords.Words do
		CW.GoodWords.Words[i]:Hide()
	end
end

CW.GoodWords.Title = CW.funcs.FontString("Good Words", 20, {rF = CW.CheckButton4, rP = "BOTTOMLEFT", x = 250, y = -10})
CW.GoodWords.Subtitle = CW.funcs.FontString("Messages from Add Ons which include a good word will always be shown.", 7, {rF = CW.GoodWords.Title, rP = "BOTTOMLEFT", y = -2})
CW.GoodWords.Subtitle:SetJustifyH"LEFT"
CW.GoodWords.EditBox = CW.funcs.Frame("EditBox", nil, CW, nil, {rF = CW.GoodWords.Subtitle, rP = "BOTTOMLEFT", y = -12})
CW.GoodWords.EditBox:SetWidth(100)
CW.GoodWords.EditBox:SetHeight(15)
CW.GoodWords.EditBox:SetBackdrop{ bgFile="Interface/ChatFrame/ChatFrameBackground" }
CW.GoodWords.EditBox:SetBackdropColor(0.1,0.1,0.1,1)
CW.GoodWords.EditBox:SetFont("Fonts\\FRIZQT__.TTF", 11)
CW.GoodWords.EditBox:SetAutoFocus(false)
CW.GoodWords.EditBox:SetScript("OnEscapePressed", function(self) self:ClearFocus() end)
CW.GoodWords.EditBox.Label = CW.funcs.FontString("Add a word", 9, { p = "BOTTOMLEFT", rF = CW.GoodWords.EditBox, y = 2})
CW.GoodWords.EditBox.Button = CreateFrame("Button", nil, CW, "OptionsButtonTemplate")
CW.GoodWords.EditBox.Button:SetPoint("LEFT", CW.GoodWords.EditBox, "RIGHT", 2, 0)
CW.GoodWords.EditBox.Button:SetWidth(30)
CW.GoodWords.EditBox.Button:SetText("Add")
CW.GoodWords.EditBox.Button:Hide()
CW.GoodWords.EditBox.Button:SetScript("OnClick", function(self) self:Hide() tinsert(ACMHOptions.goodwords, CW.GoodWords.EditBox:GetText()) CW.GoodWords.EditBox:SetText("") CW.GoodWords.EditBox:ClearFocus() RefreshGoodFontStrings() end)
CW.GoodWords.EditBox:SetScript("OnTextChanged", function(self) if self:GetText() == "" then CW.GoodWords.EditBox.Button:Hide() else CW.GoodWords.EditBox.Button:Show() end end)

CW.GoodWords.Words = {}
CW.GoodWords.Words.Title = CW.funcs.FontString("Click on a word to delete it.", 7, {rF = CW.GoodWords.EditBox, rP = "BOTTOMLEFT", y = -5})
CW.GoodWords.Words.frames = {}
--//Good Words

CW.CheckButton1:SetScript("OnClick", function()
	if load then return end
		if CW.CheckButton1:GetChecked() then
			CW.CheckButton2:Disable()
			CW.CheckButton2.FontString:SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b)
			CW.CheckButton3:Disable()
			CW.CheckButton3.FontString:SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b)
			CW.CheckButton3.FontString2:SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b)
		else 
			CW.CheckButton2:Enable()
			CW.CheckButton2.FontString:SetTextColor(HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b)
			CW.CheckButton3:Enable()
			CW.CheckButton3.FontString:SetTextColor(HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b)
			CW.CheckButton3.FontString2:SetTextColor(HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b)
	end
end)

CW:HookScript("OnSizeChanged", function()
	CW.GoodWords.Subtitle:SetWidth(CW:GetRight() - CW:GetLeft() - 255)
end)
CW:RegisterEvent"ADDON_LOADED"
local function CW_OnEvent(self, event, arg1)
	if event == "ADDON_LOADED" then
		if not ACMHOptions then
			CW.default()
		elseif not ACMHOptions.UseFilter and not ACMHOptions.UseHook then
			ACMHOptions.UseFilter = true
		end
	end
end
local function setoptions()
	if load then return end
	CW.CheckButton1:SetChecked(ACMHOptions.blockall)
	CW.CheckButton2:SetChecked(ACMHOptions.blockwhileload)
	CW.CheckButton3:SetChecked(ACMHOptions.blockafter)
	CW.CheckButton4:SetChecked(ACMHOptions.moveto)
	CW.CheckButton4.DropDown.value = (ACMHOptions.moveto or 3) - 2
	UIDropDownMenu_Initialize(CW.CheckButton4.DropDown, CW.CheckButton4.DropDown.Initfunc)
	CW.CheckButton5:SetChecked(ACMHOptions.UseFilter)
	CW.CheckButton6:SetChecked(ACMHOptions.UseHook)
	--UIDropDownMenu_SetSelectedID(CW.CheckButton4.DropDown, CW.CheckButton4.DropDown.value, 1)
	CW.CheckButton3.EditBox:SetText(ACMHOptions.blocktime)
	CW.CheckButton1:GetScript("OnClick")()
	RefreshBadFontStrings()
	RefreshGoodFontStrings()
	CW.set = true
end
CW:SetScript("OnEvent", CW_OnEvent)
CW:SetScript("OnShow", setoptions)
CW.default = function()
	ACMHOptions = {}
	ACMHOptions.blockall = nil
	ACMHOptions.blockwhileload = 1
	ACMHOptions.blockafter = 1
	ACMHOptions.blocktime = 5
	ACMHOptions.UseFilter = true
	--ACMHOptions.UseHook = false
	--ACMHOptions.moveto = false
	ACMHOptions.badwords = { [1] = "loaded"}
	ACMHOptions.goodwords = {}
	setoptions()
end
CW.okay = function()
	if CW.set then
		ACMHOptions.blockall = CW.CheckButton1:GetChecked()
		ACMHOptions.blockwhileload = CW.CheckButton2:GetChecked()
		ACMHOptions.blockafter = CW.CheckButton3:GetChecked()
		ACMHOptions.blocktime = tonumber(CW.CheckButton3.EditBox:GetText())
		ACMHOptions.moveto = CW.CheckButton4:GetChecked() == 1 and (CW.CheckButton4.DropDown.value + 2)
		ACMHOptions.UseFilter = CW.CheckButton5:GetChecked()
		ACMHOptions.UseHook = CW.CheckButton6:GetChecked()
		CW.set = nil
	end
end

InterfaceOptions_AddCategory(CW)

load = nil
